//
//  WBKeyboardCell.swift
//  WeiboTen
//
//  Created by ItHeiMa on 2017/7/18.
//  Copyright © 2017年 itHeima. All rights reserved.
//

import UIKit

fileprivate let baseTag = 999

class WBKeyboardCell: UICollectionViewCell {
    
    var models: [WBEmotionModel]? {
        didSet {
            //1. 把所有的button都隐藏起来
            for i in 0..<20 {
                self.viewWithTag(i + baseTag)?.isHidden = true
            }
            
            if let models = models {
                for (index, model) in models.enumerated() {
                    let button = self.viewWithTag(index + baseTag) as! UIButton
                    button.isHidden = false
                    
                    if model.type == 0 {
                        //装萝卜
                        button.setImage(UIImage(named: model.fullPath!), for: .normal)
                        button.setTitle(nil, for: .normal)
                    } else {
                        button.setImage(nil, for: .normal)
                        //将16进制的emoji的字符串转成emoji的动画表情字符
                        let emoji = NSString.emoji(withStringCode: model.code!)
                        button.setTitle(emoji, for: .normal)
                        button.titleLabel?.font = UIFont.systemFont(ofSize: 34)
                    }
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension WBKeyboardCell {
    func setupUI () {
        let width = screenWidth/7
        let rect = CGRect(x: 0, y: 10, width: width, height: width + 8)
        for i in 0..<21 {
            //行数
            let row = CGFloat(i / 7)
            //列数
            let col = CGFloat(i % 7)
            
            let frame = rect.offsetBy(dx: col * width, dy: row*width)
            
            let button = UIButton(text: nil, target: self, selector: #selector(addOrDeleteEmotion(button:)))
            button.frame = frame
            //button.backgroundColor = UIColor.randomColor()
            
            button.tag = i + baseTag
            
            //说明是删除按钮
            if i == 20 {
                button.setImage(UIImage(named: "compose_emotion_delete"), for: .normal)
            }
            
            self.contentView.addSubview(button)
        }
    }
}

extension WBKeyboardCell {
    @objc func addOrDeleteEmotion(button: UIButton) {
        let tag = button.tag - baseTag
        var isDelete: Bool = false
        var model: WBEmotionModel?
        if tag == 20 {
            //说明是做删除操作
            isDelete = true
        } else {
            //说明做的插入表情的操作，需要将表情的model传出去
            //取到当前点击的表情的模型
            model = models![tag]
        }
        
        //生成userInfo
        var userInfo:[String: Any] = ["isDelete": isDelete]
        userInfo["model"] = model
        
        let notification = Notification(name: emotionButtonClickNotification, object: nil, userInfo: userInfo)
        
        NotificationCenter.default.post(notification)
    }
}


